package com.gitee.clarence.business.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gitee.clarence.common.constant.Constant;
import com.gitee.clarence.common.dataobject.vo.ResultVO;
import com.gitee.clarence.entity.WrapSwagger;
import com.gitee.clarence.swagger.SwaggerSourceParse;
import com.gitee.clarence.swagger.utils.JsonUtils;
import com.gitee.clarence.util.ResultVOUtil;

import io.swagger.models.parameters.Parameter;


/**
 * 
 * @desc API信息
 *
 * @author luocheng
 *
 */
@RestController
@RequestMapping(value = "/api/v2/api-docs", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class ApiDocController {

	@Autowired
	private HttpServletRequest request;

    @Autowired
    private SwaggerSourceParse   swaggerSourceParse;

	/**
	 * 获取api
	 * 
	 * @return
	 */
	@GetMapping("")
	public String swagger() {

		WrapSwagger swagger = swaggerSourceParse.parseJarSource();
		request.setAttribute(Constant.REQUEST_RESULT_KEY, swagger);
	    return JsonUtils.toJson(swagger);
	}
	
	/**
	 * 获取所有对外方法
	 * 
	 * 
	 * @return
	 */
	@GetMapping("/methods")
	public ResultVO<List<String>> swaggerMethods() {
		
		List<String> methods = swaggerSourceParse.methods();
		request.setAttribute(Constant.REQUEST_RESULT_KEY, methods);
		return ResultVOUtil.returnSuccess(methods);
	}
	
	/**
	 * 获取对外方法对应参数
	 * 
	 * 
	 * @return
	 */
	@GetMapping("/params/{method}")
	public ResultVO<List<Parameter>> swaggerParams(@PathVariable String method) {
		
		List<Parameter> param = swaggerSourceParse.getParam(method);
		request.setAttribute(Constant.REQUEST_RESULT_KEY, param);
		return ResultVOUtil.returnSuccess(param);
	}

}
